
package format.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 將字串 parse 成為可以用的年月日, 時分秒的整數, 其字串又可分為 short, medium, and long <br>
 * Copyright: Copyright (c) 2001-2007<br>
 * Company: 一零四資訊科技股份有限公司
 * 
 * @author Eric.Kao(eric.kao@104.com.tw)
 * @version 1.0
 */
public class TestDateFormat
{
	Locale locale = Locale.TAIWAN;

	String dateShort = "2005/9/7 上午 1:20";

	// String dateShort = "上午 8:00";
	String dateMedium = "2005/9/7 上午 01:18:50";

	String dateLong = "2005年9月7日 上午12時58分47秒";

	public TestDateFormat() throws ParseException
	{
		// short
		doFormat( DateFormat.SHORT, DateFormat.SHORT, locale, dateShort, "short" );

		// medium
		doFormat( DateFormat.MEDIUM, DateFormat.MEDIUM, locale, dateMedium, "medium" );

		// long
		doFormat( DateFormat.LONG, DateFormat.LONG, locale, dateLong, "long" );
	}

	private void doFormat( int dateFormat, int timeFormat, Locale locale, String date, String label )
		throws ParseException
	{
		DateFormat format = DateFormat.getDateTimeInstance( dateFormat, timeFormat, locale );
		Date dateFormatted = format.parse( date );

		Calendar calendar = Calendar.getInstance();
		calendar.setTime( dateFormatted );

		System.out.println( "剖析(" + label + "):" + date );
		System.out.println( "年:" + calendar.get( Calendar.YEAR ) );
		System.out.println( "月:" + calendar.get( Calendar.MONTH ) );
		System.out.println( "日:" + calendar.get( Calendar.DAY_OF_MONTH ) );
		System.out.println( "時:" + calendar.get( Calendar.HOUR_OF_DAY ) );
		System.out.println( "分:" + calendar.get( Calendar.MINUTE ) );
		System.out.println( "秒:" + calendar.get( Calendar.SECOND ) );
		System.out.println();
	}

	public static void main( String[] args )
	{
		try
		{
			new TestDateFormat();
		}
		catch( ParseException ex )
		{
			ex.printStackTrace();
		}
	}

}
